package com.huqu.beans.index;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import vn.hus.nlp.tokenizer.VietTokenizer;

import com.huqu.beans.sessionWeb.SessionWeb;
import com.huqu.dao.N100Dao;
import com.huqu.dao.N200Dao;
import com.huqu.dao.N300Dao;
import com.huqu.dao.N400Dao;
import com.huqu.dao.N500Dao;
import com.huqu.dao.Q100Dao;
import com.huqu.entities.N100;
import com.huqu.entities.N200;
import com.huqu.entities.N300;
import com.huqu.entities.N400;
import com.huqu.entities.N500;
import com.huqu.entities.Q100;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;

@ManagedBean(name="indexBean")
@ViewScoped
public class IndexBean implements Serializable {
	
	private static final long serialVersionUID = 2448631731089776429L;
	
	@ManagedProperty("#{sessionWeb}")
	private SessionWeb sessionWeb;
	private String vLang="VN";
	
	private List<N200> listN200= new ArrayList<N200>();
	// nghề nghiệp dành cho bạn
	private List<N300> listN300= new ArrayList<N300>();
	private List<N300> listN300INFO= new ArrayList<N300>();
	private List<N300> listN300_news= new ArrayList<N300>();
	private N300 news_feature= new N300();
	private List<N300> listN300_question= new ArrayList<N300>();
	// videos (da bo)
	private List<N400> listN400= new ArrayList<N400>();
	// videos
	private List<N500> listN500= new ArrayList<N500>();

// ===================================================================================================================
// =====================================================FUNCTIONS=====================================================
	@PostConstruct
	public void init(){
		try {
			System.out.println("====== IndexBean =======");
			System.out.println("---sWeb.locale= "+sessionWeb.locale);
			System.out.println("---sWeb.vLang= "+sessionWeb.getvLang());
			ApplicationHelper.test();
			if(ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG)!=null){
				vLang=ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG).toString().toUpperCase();
				
				if(vLang.trim().toUpperCase().equals("VN")){
					if(sessionWeb.locale.equals(Locale.US)){
						sessionWeb.setLocale(new Locale("vi", "VN"));
						FacesContext.getCurrentInstance()
		    			.getViewRoot().setLocale(new Locale("vi", "VN"));
						RequestContext.getCurrentInstance().execute( "location.reload();");
					}
				}else if(vLang.trim().toUpperCase().equals("EN")){
					if(sessionWeb.locale.equals(new Locale("vi", "VN"))){
						sessionWeb.setLocale(Locale.US);
						FacesContext.getCurrentInstance()
		    			.getViewRoot().setLocale(Locale.US);
						RequestContext.getCurrentInstance().execute( "location.reload();");
					}
				}
			}else{
				System.out.println("---language khong xac dinh ");
			}
			
			// load danh sach các nghề nghiệp dành cho bạn
			getListOfTabN300();
			
			// load danh sach các tin tức nổi bật
			getListOfTabN300_news();
			if(listN300_news.size()>0){
				news_feature=listN300_news.get(0);
				listN300_news.remove(0);
			}
			
			// load danh sach các câu hỏi
			getListOfTabN300_question();
			
			// load danh sach các videos
			getListOfTabN500();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// load các nghề nghiệp dành cho bạn
	public void getListOfTabN300(){
		try {
			N300Dao n300Dao = (N300Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N300);
			System.out.println("---ListOfTabN300( "+"1, 0, 0, 4, 1, 1, 1, "+ vLang+" )");
			listN300=n300Dao.ListOfTabN300(1, 0, 0, 4, 1, 1, 1, vLang);
			System.out.println("ListN100.size()= "+listN300.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// load các tin tức nổi bật
	public void getListOfTabN300_news(){
		try {
			N300Dao n300Dao = (N300Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N300);
			System.out.println("---ListOfTabN300( "+"3, 0, 0, 5, 1, 1, 1, "+ vLang+" )");
			listN300_news=n300Dao.ListOfTabN300(3, 0, 0, 5, 1, 1, 1, vLang);
			System.out.println("ListN300_news.size()= "+listN300_news.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// load các câu hỏi đáp
	public void getListOfTabN300_question(){
		try {
			N300Dao n300Dao = (N300Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N300);
			System.out.println("---ListOfTabN300( "+"2, 0, 0, 5, 1, 1, 1, "+ vLang+" )");
			listN300_question=n300Dao.ListOfTabN300(2, 0, 0, 5, 1, 1, 1, vLang);
			System.out.println("ListN300_question.size()= "+listN300_question.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// load các video
	public void getListOfTabN400(){
		try {
			N400Dao n400Dao = (N400Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N400);
			System.out.println("---ListOfTabN400( "+"1, 0, 0, 4, 1, 1, 1, "+ vLang+" )");
			listN400=n400Dao.ListOfTabN400(0,4);
			System.out.println("ListN400.size()= "+listN400.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// load các video
	public void getListOfTabN500(){
		try {
			N500Dao n500Dao = (N500Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N500);
			System.out.println("---ListOfTabN500(0, 0, 4, 1, 0, "+ vLang+" )");
			listN500=n500Dao.ListOfTabN500(0, 0, 4, 1, 0, vLang);
			System.out.println("ListN500.size()= "+listN500.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
//--------------------------------------------------- GET-SET -------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

	public List<N300> getListN300() {
		return listN300;
	}
	public void setListN300(List<N300> listN300) {
		this.listN300 = listN300;
	}
	
	public SessionWeb getSessionWeb() {
		return sessionWeb;
	}
	public void setSessionWeb(SessionWeb sessionWeb) {
		this.sessionWeb = sessionWeb;
	}

	public String getvLang() {
		return vLang;
	}
	public void setvLang(String vLanguage) {
		vLang = vLanguage;
	}

	public List<N300> getListN300_news() {
		return listN300_news;
	}

	public void setListN300_news(List<N300> listN300_news) {
		this.listN300_news = listN300_news;
	}
	
	public N300 getNews_feature() {
		return news_feature;
	}

	public void setNews_feature(N300 news_feature) {
		this.news_feature = news_feature;
	}

	public List<N300> getListN300_question() {
		return listN300_question;
	}

	public void setListN300_question(List<N300> listN300_question) {
		this.listN300_question = listN300_question;
	}

	public List<N400> getListN400() {
		return listN400;
	}

	public void setListN400(List<N400> listN400) {
		this.listN400 = listN400;
	}

	public List<N500> getListN500() {
		return listN500;
	}

	public void setListN500(List<N500> listN500) {
		this.listN500 = listN500;
	}

}
